1704C - Virus - CodeForces Solution


greedy sortings

Please click on ads to support us..

Python Code:

def solve():
    n, m = readIntArr()
    a = readIntArr()
    a.sort()
            gaps = []
    for i in range(m):
        x, y = a[i % m], a[(i + 1) % m]
        if x >= y:
            x -= n
        if y - x - 1 > 0:
            gaps.append(y - x - 1)
    gaps.sort(reverse=True)
        
    savedtotal = 0
    subtract = 0
    for x in gaps:
        saved = x - 2 * subtract
                if saved <= 0:
            continue
        if saved <= 2:
            savedtotal += 1
            subtract += 1          else:
            savedtotal += saved - 1
            subtract += 2
    ans = n - savedtotal
    return ans

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        allans.append(solve())
    multiLineArrayPrint(allans)
    
    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include<bits/stdc++.h>
#define int long long 
using namespace std;

void char_2d(vector<vector<char>> &v, int n, int m){for(int i = 0; i<n; i++){for(int j = 0; j<m; j++)  cout<<v[i][j]; cout<<endl;}}
void int_2d(vector<vector<char>> &v, int n, int m){for(int i = 0; i<n; i++){for(int j = 0; j<m; j++)  cout<<v[i][j]; cout<<endl;}}
void char_1d(vector<char> &v){for(int i = 0; i<v.size(); i++){cout<<v[i]<<" ";}cout<<endl;}



void solve()
{
    int n, m;
    cin>>n>>m;
    vector<int> v(m);
    int temp = 0, safe = 0;
    for(int i = 0; i<m; i++){
        cin>>v.at(i);
    }
    sort(v.begin(), v.end());
    vector<int> diff;
    diff.push_back(v[0]-1 + n-v[m-1]);
    for(int i = 0; i<m-1; i++){
        diff.push_back(v[i+1]-v[i]-1);
    }
    sort(diff.begin(), diff.end(), greater<int>());
    for(int i = 0; i<diff.size(); i++){
        int ans = diff[i];
        ans -= (2*temp);
        if(ans <= 0) break;
        safe += (ans-1);
        if(ans == 1) safe++;
        temp += 2;
    }

    cout<<n-safe<<endl;
    

}
     
     
    int32_t main()
    {
        int t;
        cin>>t;

        vector<bool> isprime(100000, true);

        while(t--)
        {
            solve();
     
        }
    }



Comments

Submit
0 Comments
More Questions

1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array
1085A - Right-Left Cipher
1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum